<!--
 * @Author: 撒哈拉的花儿 30940518@qq.com
 * @Date: 2024-10-27 22:38:49
 * @LastEditors: 撒哈拉的花儿 30940518@qq.com
 * @LastEditTime: 2024-11-23 21:53:45
 * @FilePath: \bl_portal_foreend\src\views\bl\order\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户" prop="deptId">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择商户">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="下单时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in bl_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货状态" prop="shippingStatus">
        <el-select v-model="queryParams.shippingStatus" placeholder="请选择发货状态" clearable>
          <el-option
            v-for="dict in bl_shipping_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in bl_pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入收货人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-row justify="center">
        <el-form-item >
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row> 
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="false">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['bl:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bl:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bl:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['bl:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" row-key="orderId" @selection-change="handleSelectionChange" stripe style="width: 100%">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="orderSn" width="150"/>
      <el-table-column label="支付状态" align="center" prop="payStatus" width="78">
        <template #default="scope">
          <dict-tag :options="bl_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="发货状态" align="center" prop="shippingStatus" width="78">
        <template #default="scope">
          <dict-tag :options="bl_shipping_status" :value="scope.row.shippingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="78">
        <template #default="scope">
          <dict-tag :options="bl_order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="收货人" align="center" prop="consignee" width="90"/>
      <el-table-column label="收货地址" align="center" prop="address" show-overflow-tooltip="true"/>
      <el-table-column label="手机" align="center" prop="mobile" width="110"/>
      <el-table-column label="支付方式名称" align="center" prop="payName" v-if="false"/>
      <el-table-column label="应付款金额" align="center" prop="orderAmount" width="90" v-if="false"/>
      <el-table-column label="订单总价" align="center" prop="totalAmount"  width="78"/>
      <el-table-column label="下单时间" align="center" prop="addTime" width="155">
        <template #default="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="155">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货确认时间" align="center" prop="confirmTime" width="155">
        <template #default="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户备注" align="center" prop="userNote" show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="50">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['bl:order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bl:order:remove']" v-if="false">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="详情" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
        <template #default="scope">
          <router-link :to="'/bl/order-goods/index/' + scope.row.orderId" class="link-type">
            <span>订单详情</span>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发货状态" prop="shippingStatus">
          <el-select v-model="form.shippingStatus" placeholder="请选择发货状态">
            <el-option
              v-for="dict in bl_shipping_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in bl_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/bl/order/order";
import { listDeptAll } from "@/api/bl/dept";

const { proxy } = getCurrentInstance();
const { bl_order_status, bl_shipping_status, bl_pay_status } = proxy.useDict('bl_order_status', 'bl_shipping_status', 'bl_pay_status');

const orderList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const deptOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    orderSn: null,
    orderStatus: null,
    shippingStatus: null,
    payStatus: null,
    consignee: null,
    address: null,
    mobile: null,
    addTime: null,
    confirmTime: null,
    payTime: null,
    userNote: null,
  },
  rules: {
    orderStatus: [
      { required: true, message: "订单状态不能为空", trigger: "change" }
    ],
    shippingStatus: [
      { required: true, message: "发货状态不能为空", trigger: "change" }
    ],
    payStatus: [
      { required: true, message: "支付状态不能为空", trigger: "change" }
    ],
    consignee: [
      { required: true, message: "收货人不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    mobile: [
      { required: true, message: "手机不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单信息列表 */
function getList() {
  if(queryParams.value.deptId === null || queryParams.value.deptId === undefined){
    return;
  }
  loading.value = true;
  listOrder(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderSn: null,
    deliveryOrderId: null,
    userId: null,
    orderStatus: null,
    shippingStatus: null,
    payStatus: null,
    consignee: null,
    address: null,
    mobile: null,
    email: null,
    areaCode: null,
    areaName: null,
    payCode: null,
    payName: null,
    orderAmount: null,
    totalAmount: null,
    addTime: null,
    confirmTime: null,
    payTime: null,
    userNote: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orderId = row.orderId || ids.value
  getOrder(_orderId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value;
  proxy.$modal.confirm('是否确认删除订单信息编号为"' + _orderIds + '"的数据项？').then(function() {
    return delOrder(_orderIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bl/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

    /** 查询商户信息列表 */
function getDeptAllList() {
  listDeptAll(queryParams.value).then(response => {
    let deptListTmp = response.data;
    deptOptions.value = deptListTmp.filter(item => item.deptId !== 100);
  });
}

getDeptAllList();
//getList();
</script>
